<?php


namespace app\home\controller;
use app\home\model\PgsgaHuanzhe as PgsgaHuanzheModel;

use think\Controller;
use think\Request;

class PgsgaHuanzhe extends Controller
{
    protected $pgsgaHuanzheModel;

    public function __construct(
        PgsgaHuanzheModel $pgsgaHuanzheModel
    )
    {
        parent::__construct();
        $this->pgsgaHuanzheModel = $pgsgaHuanzheModel;
    }

    public function show(Request $request)
    {
        return $this->fetch();
    }

    public function lists()
    {
        return $this->fetch();
    }

    public function ajax_lists(Request $request)
    {
        $page = $request->param('page', 1);
        $limit = $request->param('limit', 10);
        $where = [];
        $data = $this->pgsgaHuanzheModel->where($where)->page($page, $limit)->select();
        $count = $this->pgsgaHuanzheModel->where($where)->count();
        return reorganizeTableData(0, '', $count, $data);
    }

    public function info(Request $request)
    {
        $pid = $request->param('pid', '');
        $res = $this->pgsgaHuanzheModel->where('pid', $pid)->find();
        if(isset($res['zhengzhuang_text']) && !empty($res['zhengzhuang_text'])){
            $res['zhengzhuang_text'] = json_decode($res['zhengzhuang_text'],1);
        }
        return jsonRes($res);
    }

    public function update(Request $request)
    {
        $pid = $request->param('pid', '');
        if(!isset($pid) || empty($pid)){
            return jsonRes([],'pid不能为空',99);
        }
        $tizhong = $request->param('tizhong', '');
        $tizhong1 = $request->param('tizhong1', '');
        $tizhong6 = $request->param('tizhong6', '');
        $tizhongbianhua = $request->param('tizhongbianhua', 0);
        $tizhongpingfen_type = $request->param('tizhongpingfen_type', 0);
        $tizhongpingfen = $request->param('tizhongpingfen', 0);
        $tizhongxiajiang = $request->param('tizhongxiajiang', 0);
        $jinshibianhua = $request->param('jinshibianhua', 0);
        $jinshi = $request->param('jinshi', 0);
        $zhengzhuang = $request->param('zhengzhuang', '');
        $zhengzhuang_text = $request->param('zhengzhuang_text', '');
        $huodong = $request->param('huodong', 0);

        $data = [
            'pid' => $pid,
            'tizhong' => $tizhong,
            'tizhong1' => $tizhong1,
            'tizhong6' => $tizhong6,
            'tizhongbianhua' => $tizhongbianhua,
            'tizhongpingfen_type' => $tizhongpingfen_type,
            'tizhongpingfen' => $tizhongpingfen,
            'tizhongxiajiang' => $tizhongxiajiang,
            'jinshibianhua' => $jinshibianhua,
            'jinshi' => $jinshi,
            'zhengzhuang' => $zhengzhuang,
            'zhengzhuang_text' => json_encode($zhengzhuang_text,1),
            'huodong' => $huodong
        ];

        //判断数据是否存在
        $info = $this->pgsgaHuanzheModel->where('pid', $pid)->with(['huanzhe' => function ($query) {
            $query->field('id,hospital_id,keshi,name,gender,birthday,created');
        }])->findOrEmpty()->toArray();

        if(empty($info)){
            $res = $this->pgsgaHuanzheModel->save($data);
        }else{
            $res = $this->pgsgaHuanzheModel->save($data,['pid' => $info['pid']]);
        }

        if ($res) {
            $resBack = jsonRes($res);
        } else {
            $resBack = jsonRes($res, '更新失败', 99);
        }
        return $resBack;
    }
}